package com.core.aspectj;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.core.aspectj.permission.SkyPermission;
import com.core.aspectj.ui.AspectjPermissionActivity;
import com.core.aspectj.util.AopLog;
import com.core.aspectj.util.PermissionUtil;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.util.ArrayList;

@Aspect
public class AnnotationAspectTrace {

    private final String TAG = this.getClass().getSimpleName();

    /**
     * 针对带有AppLog注解的方法
     */
    @Pointcut("execution(@com.core.aspectj.logger.AppLog * *(..))")
    public void aspectDebugLogAnnotation() {
    }

    @Pointcut("execution(@com.core.aspectj.auth.UserAuth * *(..))")
    public void aspectAuthenticationAnnotation() {
    }


    @Pointcut("execution(@com.core.aspectj.permission.SkyPermission * *(..))")
    public void aspectSkyPermissionAnnotation() {

    }

    /**
     * 针对前面 aspectSkyPermissionAnnotation()  的配置
     */
    @Around("aspectSkyPermissionAnnotation()")
    public void aspectSkyPermission(final ProceedingJoinPoint joinPoint) throws Throwable {

        SkyPermission systemLog = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(SkyPermission.class);
        String list[] = systemLog.list();
        for (String s : list) {
            Log.i("AspectTrace", "aspectSkyPermission-->" + s);
        }
        joinPoint.proceed();

        Object object = joinPoint.getThis();
        Log.i("AspectTrace", "aspectSkyPermission-->" + object.getClass().getSimpleName());
        if (object instanceof Activity) {
            final Context context = Lite.getAppContext();
            ArrayList<String> arrays = new ArrayList();
            for (String itemCheck : list) {
                if (!PermissionUtil.checkPermission(context, itemCheck)) {
                    arrays.add(itemCheck);
                }
            }
            Log.i("AspectTrace", "aspectSkyPermission-->List isEmpty="+arrays.isEmpty());
            if(!arrays.isEmpty()){
                AspectjPermissionActivity.start((Activity) object, arrays);
            }
        }
        //
    }

    /**
     * 针对前面 aspectDebugLogAnnotation()  的配置
     */
    @Around("aspectDebugLogAnnotation()")
    public void aspectDebugLogAnnotation(final ProceedingJoinPoint joinPoint) throws Throwable {
        long startTimeMillis = System.currentTimeMillis();
        AopLog.iFront(joinPoint);
        joinPoint.proceed();
        AopLog.iAfter(joinPoint);
    }


    /**
     * 针对前面 aspectDebugLogAnnotation()  的配置
     */
    @Around("aspectAuthenticationAnnotation()")
    public void aspectAuthentication(final ProceedingJoinPoint joinPoint) throws Throwable {
        joinPoint.proceed();
        if (!Lite.isUserAuth()) {
            Lite.startLoginActivity();
        }
    }

}
